<?php
	include('template/header.php');
	include('classess/user_subscribe.php');
	function output_file($file, $name, $mime_type='',$ebk_count,$value_id)
	{
		if(!is_readable($file)) die('File not found or inaccessible!');

		$size = filesize($file);
		$name = rawurldecode($name);
		$known_mime_types=array(
		"pdf" => "application/pdf",
		"txt" => "text/plain",
		"html" => "text/html",
		"htm" => "text/html",
		"exe" => "application/octet-stream",
		"zip" => "application/zip",
		"doc" => "application/msword",
		"xls" => "application/vnd.ms-excel",
		"ppt" => "application/vnd.ms-powerpoint",
		"gif" => "image/gif",
		"png" => "image/png",
		"jpeg"=> "image/jpg",
		"jpg" => "image/jpg",
		"php" => "text/plain"
		);
		if($mime_type==''){
			$file_extension = strtolower(substr(strrchr($file,"."),1));
			if(array_key_exists($file_extension, $known_mime_types)){
				$mime_type=$known_mime_types[$file_extension];
			} else {
				$mime_type="application/force-download";
			};
		};

		@ob_end_clean();


		if(ini_get('zlib.output_compression'))
		ini_set('zlib.output_compression', 'Off');
		header('Content-Type: ' . $mime_type);
		header('Content-Disposition: attachment; filename="'.$name.'"');
		header("Content-Transfer-Encoding: binary");
		header('Accept-Ranges: bytes');
		header("Cache-control: private");
		header('Pragma: private');
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		if(isset($_SERVER['HTTP_RANGE']))
		{
			list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
			list($range) = explode(",",$range,2);
			list($range, $range_end) = explode("-", $range);
			$range=intval($range);
			if(!$range_end) {
				$range_end=$size-1;
			} else {
				$range_end=intval($range_end);
			}
			$new_length = $range_end-$range+1;
			header("HTTP/1.1 206 Partial Content");
			header("Content-Length: $new_length");
			header("Content-Range: bytes $range-$range_end/$size");
		} else {
			$new_length=$size;
			header("Content-Length: ".$size);
		}
		$chunksize = 1*(1024*1024);
		$bytes_send = 0;
		if ($file = fopen($file, 'r'))
		{
			if(isset($_SERVER['HTTP_RANGE']))
			fseek($file, $range);

			while(!feof($file) &&
			(!connection_aborted()) &&
			($bytes_send<$new_length)
			)
				{
				$buffer = fread($file, $chunksize);
				print($buffer);
				flush();
				$bytes_send += strlen($buffer);
			}	
			
			$update_ebook_count = "UPDATE tbl_user_details SET ebook_count='".$ebk_count."' WHERE id='".$value_id."'";
			$query_ebook_count = mysql_query($update_ebook_count) or die(mysql_error());
			fclose($file);
		} else

		die('Error - can not open file.');
		die();
	}
?>
<section  class="homepage-slider" id="home-slider">
		<div class="flexslider">
			<ul class="slides">
				<li>
					<img src="img/slider/first_image.jpg" alt="No Image" />					
				</li>
				<li>
					<img src="img/slider/second_image.jpg" alt="No Image" />					
				</li>
			</ul>
		</div>			
	</section>
	<section class="main-content">
		<div class="row">
			
			<div class="span9">													
				<div class="row">
					<div class="span9">
						<h4 class="title">
							<span class="pull-left"><span class="text"><span class="line"><strong>Download Ebook</strong></span></span></span>
							<span class="pull-right">
								&nbsp;
							</span>
						</h4>
						<div id="myCarousel" class="myCarousel carousel slide">
							<div class="carousel-inner">
								<div class="active item">
									
									<div class="content_content_text">
										<?php
											if(isset($_GET['download_link'])){
												$val = explode('.',$_GET['download_link']);
												$select_user = "SELECT * FROM tbl_user WHERE reference_url='".$val['0']."'";
												$query_user = mysql_query($select_user) or die(mysql_error());

												if(mysql_num_rows($query_user)>0){													
													$user_id = '';
													while($row_user = mysql_fetch_array($query_user)){
														$user_id = $row_user['id'];
													}

													if(isset($val['1'])){
														$select_user_dtl = "SELECT * FROM tbl_user_details WHERE id='".$val['1']."' AND user_id='".$user_id."'";
														$query_user_dtl = mysql_query($select_user_dtl) or die(mysql_error());
														if(mysql_num_rows($query_user_dtl)>0){
															while($row = mysql_fetch_array($query_user_dtl)){	
																$ebk_count = '';
																if($row['is_ebook'] == 1){
																	if($row['ebook_count']<$row['ebook_download_time']){
																		$ebk_count = $row['ebook_count'];
																		$ebook_id = $row['course_id'];
																		$select_ebook = "SELECT * FROM tbl_ebook WHERE id='".$ebook_id."'";
																		$query_ebook = mysql_query($select_ebook) or die(mysql_error());
																		if(mysql_num_rows($query_ebook)>0){
																			$file = '';
																			while($row_ebook = mysql_fetch_array($query_ebook)){
																				$file = $row_ebook['ebook_file'];
																				
																			}
																			$ebk_count++;
																			$extension = explode('.',$file);
																			$file_path='admin/dashboard/ebook_files/'.$ebook_id.'/'.$ebook_id.'.'.$extension['1'];
																			output_file($file_path, ''.$file.'', 'text/plain',$ebk_count,$val['1']);
																		}
																	}else{
																		echo 'This File IS downloaded';
																	}
																}else{
																	echo 'Wrong URL';
																}
															}
														}else{
															echo 'Wrong URL';
														}
													}else{
														echo 'Wrong URL';
													}
												}else{
													echo 'Wrong URL';
												}
											}else{
												echo 'Wrong URL';
											}
										?>
									</div>

								</div>
							</div>							
						</div>

					</div>						
				</div>	
			</div>
			<?php include('template/right_panel.php'); ?>
		</div>
	</section>
<?php include('template/footer.php'); ?>